#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

    start_timer = new QTimer(this);
    connect(start_timer, SIGNAL(timeout()), this, SLOT(expand_window()));

    close_timer = new QTimer(this);
    connect(close_timer, SIGNAL(timeout()), this, SLOT(narrow_window()));

    setInfo(this,win_info);

    addInfo(ui->pushButton);
    addInfo(ui->label);
    addInfo(ui->progressBar);

    this->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);

}

Form::~Form()
{
    delete ui;
}

void Form::update_parts(double ratio)
{
    for(int i=0;i<parts.size();i++)
    {
        update_part(parts[i].subw,ratio,parts[i]);
    }
}

void Form::update_part(QWidget *w, double ratio, Info info)
{
    w->setGeometry(info.x*ratio,info.y*ratio,info.w*ratio,info.h*ratio);
    QFont font=w->font();
    font.setPointSize(info.font_size*ratio);
    w->setFont(font);
}

void Form::expand_window()
{
    int x=px+stepx*showTimes;
    int y=py+stepy*showTimes;
    int w=showTimes*stepw;
    int h=showTimes*steph;
    this->setGeometry(x,y,w,h);
    this->show();

    update_parts(showTimes/STEP_NUM);

    add_showTimes();
    if(showTimes>STEP_NUM)
    {
        start_timer->stop();
    }

    ui->progressBar->setValue(100*showTimes/STEP_NUM);
}

void Form::narrow_window()
{
    int x=px+stepx*showTimes;
    int y=py+stepy*showTimes;
    int w=showTimes*stepw;
    int h=showTimes*steph;
    this->setGeometry(x,y,w,h);

    update_parts(showTimes/STEP_NUM);

    sub_showTimes();
    if(showTimes<=0)
    {
        close_timer->stop();
        this->close();
    }
    ui->progressBar->setValue(100*showTimes/STEP_NUM);
}


void Form::start_show()
{
    showTimes=0;
    close_timer->stop();
    start_timer->start(SHOW_TIME/STEP_NUM);
}

void Form::close_show()
{
    start_timer->stop();
    close_timer->start(SHOW_TIME/STEP_NUM);
}

void Form::on_pushButton_clicked()
{
    close_show();
}

void Form::paintEvent(QPaintEvent *) {

    QPainter painter(this); // this 是 的指针
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);

    int h=this->height();
    int w=this->width()*showTimes/STEP_NUM;

    painter.drawRect(0,0,w,h);
}
